package com.leve.control.service;

import com.leve.control.domain.Cuser;
import com.leve.control.domain.CuserDevice;
import com.leve.control.domain.Device;
import com.leve.control.domain.DeviceCat;
import com.leve.control.mapper.CuserDeviceMapper;
import com.leve.control.mapper.DeviceCatMapper;
import com.leve.control.mapper.DeviceMapper;
import com.leve.control.util.UuidUtil;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author whongyu
 * @create by 2019/6/13
 */
@Service
public class DeviceService {

    @Resource
    private DeviceMapper deviceMapper;
    @Resource
    private CuserDeviceMapper cuserDeviceMapper;
    @Autowired
    private CuserService cuserService;
    @Resource
    private DeviceCatMapper deviceCatMapper;

    /**
     * 查看用户设备列表
     * @param userId
     * @return
     */
    public List<Device> findDevice( String userId,int permission){
        return deviceMapper.findDevice(userId,permission);
    }

    /**
     * 设备信息
     * @param Id
     * @return
     */
    public Map<String,Object> queryDevice(int Id){
        Device device = deviceMapper.queryDevice(Id);
        List<CuserDevice> devicesControllers = cuserDeviceMapper.queryDeviceByDeviceId(Id, 1);//操控者
        List<CuserDevice> devicesObserver = cuserDeviceMapper.queryDeviceByDeviceId(Id, 2);//观察者
        Map<String,Object> map = new HashMap<>();
        map.put("device",device);
        map.put("devicesControllers",devicesControllers);
        map.put("devicesObserver",devicesObserver);
        return map;
    }

    public int deviceImpower(CuserDevice cuserDevice){
        return cuserDeviceMapper.update(cuserDevice);
    }

    public int removeDeviceRele(int id){
        return cuserDeviceMapper.remove(id);
    }


    public int addDeviceCuser(CuserDevice cuserDevice){
        Cuser cuser = cuserService.findByMobile(cuserDevice.getMobile());
        if (null == cuser){
            cuser = new Cuser();
            cuser.setCuserId(UuidUtil.getUUID());
            cuser.setCreateTime(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
            cuser.setMobile(cuserDevice.getMobile());
            cuserService.addCuser(cuser);//返回了主键
        }
        CuserDevice cuserDevice1 = new CuserDevice();
        cuserDevice1.setCuserId(cuser.getCuserId());
        cuserDevice1.setDeviceId(cuserDevice.getDeviceId());
        cuserDevice1.setPermission(cuserDevice.getPermission());
        cuserDevice1.setRemarkName(cuserDevice.getRemarkName());

        return cuserDeviceMapper.add(cuserDevice1);
    }

    public int addDevice(Device device){
        int i = deviceMapper.add(device);
        DeviceCat deviceCat = new DeviceCat();
        deviceCat.setDeviceId(device.getIid());
        deviceCat.setIotDeviceId(device.getIotDeviceId());
        deviceCat.setIotDeviceSecret(device.getIotDeviceSecret());
        int j = deviceCatMapper.add(deviceCat);
        return j+i;
    }

    public int editDevice(Device device){
        int i = deviceMapper.update(device);
        DeviceCat deviceCat = deviceCatMapper.getDeviceCatByDeviceId(device.getIid());
        deviceCat.setDeviceId(device.getIid());
        deviceCat.setIotDeviceId(device.getIotDeviceId());
        deviceCat.setIotDeviceSecret(device.getIotDeviceSecret());
        int j = deviceCatMapper.update(deviceCat);
        return j+i;
    }

}
